define(["jquery", "core", "mask", "tip"], function(jquery, core, mask, tip){
	var Box, BoxUtils;
	
	Box = function(type, message, title, settings, parent){
		this.type = type;
        this.settings = $.extend({}, Box.defaultSettings, settings);
        this.message = message;
        this.title = title;
        this.parent = core.getParent(parent);
	};
	
	Box.initAlert = function(box){
		box._sureBtn = box._contentElement.find(".app-box-button-btn-sure");
		box._cancelBtn = box._contentElement.find(".app-box-button-btn-cancel");
		if(box.settings.sureBtnShow){
			box._sureBtn.text("确定").click(function(){
				box.close();
				box.settings.clickSure();
			}).show();
		}
		if(box.settings.cancelBtnShow){
			box._cancelBtn.text("取消").click(function(){
				box.close();
				box.settings.clickCancel();
			}).show();
		}
		if(box.settings.autoClose){
			setTimeout(function(){box.close();}, box.settings.autoCloseDelay);
		}
	};
	
	Box.initConfirm = function(box){
		box._yesBtn = box._contentElement.find(".app-box-button-btn-yes");
		box._noBtn = box._contentElement.find(".app-box-button-btn-no");
		if(box.settings.yesBtnShow){
			box._yesBtn.text("是").click(function(){
				box.close();
				box.settings.clickYes();
			}).show();
		}
		if(box.settings.noBtnShow){
			box._noBtn.text("否").click(function(){
				box.close();
				box.settings.clickNo();
			}).show();
		}
	};
	
	Box.initPrompt = function(box){
		box._contentElement.find(".app-box-alert-image").hide();
		box._contentElement.find(".app-box-message").css({
			"left": "10px",
			"width": (box.settings.width - 20) + "px",
			"height": "20px",
			"margin-top": "12px"
		});
		box._sureBtn = box._contentElement.find(".app-box-button-btn-sure");
		box._cancelBtn = box._contentElement.find(".app-box-button-btn-cancel");
		var input = $("<div class='app-box-prompt-text'><input type='text'></div>");
		input.appendTo(box._contentElement);
		input.css({
			"width": (box.settings.width - 20) + "px",
		});
		if(box.settings.sureBtnShow){
			box._sureBtn.text("确定").click(function(){
				var value = input.find("input").val();
				if (box.settings.validate() && !box.settings.validate(value, box))
					return;
				box.close();
				box.settings.clickSure(value);
			}).show();
		}
		if(box.settings.cancelBtnShow){
			box._cancelBtn.text("取消").click(function(){
				var value = input.find("input").val();
				box.close();
				box.settings.clickCancel(value);
			}).show();
		}
	};
	
	Box.prototype.init = function(){
		//内部属性
		var that = this;
		this.boxElement = $(Box.view);
		this._headElement = this.boxElement.find(".app-box-head");
		this._contentElement = this.boxElement.find(".app-box-content");
		//遮罩
		this._mask = mask.getInstance(that.parent);
		this._mask.show();
		this._contentElement.find(".app-box-message").html(that.message);
		//box
		this.boxElement.css({
			"width": that.settings.width,
			"height": that.settings.height,
			"left": (that.parent.width() - that.settings.width)/2,
			"top": ((that.parent.selector == 'body' ? document.documentElement.clientHeight : 
					(that.parent.height() === 0 ? document.documentElement.clientHeight : that.parent.height()))  - that.settings.height)/2,
			"z-index": mask.getMaskNextZIndex()
		});
		//设置内容
		this._contentElement.css({
			"opacity": that.settings.contentOpacity,
			"height": that.boxElement.height() - that._headElement.height() 
		});
		this.uniqueID = core.generateId("app-box");
		this._contentElement.attr("id", that.uniqueID);
		this.boxElement.appendTo(that.parent);
		//设置标题
		this._headElement.find(".app-box-head-title").text(that.title);
		this._headElement.css({"opacity": that.settings.headOpacity});
		//关闭操作
		this._headElement.find(".app-box-head-button-btn-close").click(function(){
			that.close();
		}).attr("title", "关闭");
		//如果消息内容过多，重新设置面板高度
		var messageHeight = this._contentElement.find(".app-box-message").height();
		if(messageHeight > 40){
			that.settings.height = that.settings.height - 40 + messageHeight;
		}
		that.resize();
		if(this.type === "alert"){
			Box.initAlert(this);
		}else if(this.type === "prompt"){
			Box.initPrompt(this);
		}else if(this.type === "confirm"){
			Box.initConfirm(this);
		}
	};

	Box.prototype.resize = function(width, height){
		var that = this;
		that.boxElement.css({
			"width": width || that.settings.width,
			"height": height || that.settings.height,
			"left": (that.parent.width() - (width || that.settings.width)) / 2,
			"top": ((that.parent.selector == 'body' ? document.documentElement.clientHeight :
				(that.parent.height() === 0 ? document.documentElement.clientHeight : that.parent.height())) - (height || that.settings.height)) / 2
		});
		that._contentElement.css({
			"height": that.boxElement.height() - that._headElement.height() + 1
		});
	};

	Box.prototype.showError = function(text) {
		tip.showError(text, null, 5, this.boxElement, 15);
	};
	
	/**
	 * 点击sure 或 yes 按钮
	 */
	Box.prototype.clickSureOrYes = function(){
		if(this._sureBtn){
			this._sureBtn.click();
		}
		if(this._yesBtn){
			this._yesBtn.click();
		}
	};
	
	Box.prototype.close = function(){
		this._mask.remove();
		this.boxElement.remove();
	};
	
	Box.defaultSettings = {
		width: 320,
		height: 150,
		sureBtnShow: true,
		cancelBtnShow: true,
		yesBtnShow: true,
		noBtnShow: true,
		contentOpacity: 0.9,  //内容透明度
		headOpacity: 1,       //标题透明度
		autoClose: false,	//自动关闭
		autoCloseDelay: 1500, //自动关闭的延迟时间
		validate: function(){},
		clickSure: function(){},
		clickCancel: function(){},
		clickYes: function(){},
		clickNo: function(){}
	};
	
	Box.view = [
  		"<div class='app-box'>",
  		"<div class='app-box-head app-box-head-spe'>",
  			"<span class='app-box-head-title'></span>",
  			"<div class='app-box-head-button'>",
  			    "<svg width='20' height='22' version='1.1' xmlns='http://www.w3.org/2000/svg' class='app-box-head-button-btn app-box-head-button-btn-close'>",
  			        //"<circle cx='8' cy='10' r='6' stroke='#FFFFFF' stroke-width='2' fill='red'/>",
  			        "<line x1='5' y1='5' x2='15' y2='15'  style='stroke:white;stroke-width:2'/>",
  			        "<line x1='15' y1='5' x2='5' y2='15'  style='stroke:white;stroke-width:2'/>",
  			    "</svg>",
  			"</div>",
  		"</div>",
  		"<div class='app-box-content app-box-content-spe' id=''>",
	  		"<div class='app-box-alert-image'>",
				"<svg width='80' height='70' version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px'" ,
				" viewBox='0 0 512 512' style='enable-background:new 0 0 512 512;' xml:space='preserve'>" ,
				"<path style='fill:#FF9F19;' d='M445.215,478.835H66.848c-23.67,0-45.079-12.118-57.25-32.418\n" ,
				"c-12.182-20.3-12.802-44.883-1.663-65.781L198.889,65.387c12.182-20.17,33.548-32.222,57.142-32.222s44.959,12.052,57.152,32.245" ,
				"l190.499,314.433c11.585,21.692,10.966,46.274-1.217,66.575C490.294,466.717,468.885,478.835,445.215,478.835z'/>" ,
				"<path style='fill:#F28618;' d='M445.215,478.835c23.67,0,45.079-12.118,57.25-32.418c12.182-20.3,12.802-44.883,1.217-66.575" ,
				"L313.184,65.409c-12.193-20.193-33.559-32.245-57.152-32.245v445.67H445.215z'/>" ,
				"<path style='fill:#486475;' d='M256.031,345.294c-9.215,0-16.693-7.477-16.693-16.693V161.675c0-9.215,7.477-16.693,16.693-16.693\n" ,
				"s16.693,7.477,16.693,16.693v166.926C272.724,337.817,265.247,345.294,256.031,345.294z'/>" ,
				"<path style='fill:#3C4D5C;' d='M256.031,412.061c-9.21,0-16.693-7.498-16.693-16.693s7.483-16.693,16.693-16.693" ,
				"c9.21,0,16.693,7.498,16.693,16.693S265.241,412.061,256.031,412.061z'/>" ,
				"</svg>",
	  		"</div>",
	  		"<div class='app-box-message'></div>",
	  		"<div class='app-box-button'>",
	  			"<div class='app-button app-button-middle app-box-button-btn-sure'></div>",
	  			"<div class='app-button app-button-middle app-box-button-btn-cancel'></div>",
	  			"<div class='app-button app-button-middle app-box-button-btn-yes'></div>",
	  			"<div class='app-button app-button-middle app-box-button-btn-no'></div>",
	  		"</div>", 
  		"</div>",
  		"</div>"     
  	].join("");
	
	BoxUtils = {
		alert: function(message, title, settings, parent){
			if(settings == undefined){
				settings = {cancelBtnShow: false};
			} else {
				settings.cancelBtnShow = false;
			}
			
			var box = new Box("alert", message, title, settings, parent);
			box.init();
			return box;
		},
		confirm: function(message, title, settings, parent){
			var box = new Box("confirm", message, title, settings, parent);
			box.init();
			return box;
		},
		prompt: function(message, title, settings, parent){
			var box = new Box("prompt", message, title, settings, parent);
			box.init();
			return box;
		}
	}
	
	return BoxUtils;
});